<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends BaseController {
	//管理员列表
	public function index(){		
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'管理员列表',
				'url'=>U('Admin/User/index')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('admin_user');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		if(I('name')){
			$search=I('name');
			$map['name']=array('like',"%$search%");
		}
		$cache=$m->where($map)->page($p,$psize)->order('id asc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '角色管理','bb-search');
		$role=M('admin_auth_group')->field('id,title')->where(array('status'=>'1'))->order('id asc')->select();
		foreach ($cache as $k => $v) {
			if($_SESSION['admin']['user']['role']!='1'){
				unset($cache[0]);
			}
			foreach ($role as $key => $value){
				if($value['id']==$v['role']){	$cache[$k]['rolename']=$value['title'];	}
			}
		}
		$this->assign('cache',$cache);
		$this->display();
	}
	
	//管理员设置
	public function set(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'管理员列表',
				'url'=>U('Admin/User/index')
			),
			'2'=>array(
				'name'=>'管理员设置',
				'url'=>U('Admin/User/set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('admin_user');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			if($id){
				if($p['pass']){
					if($p['pass']==$p['pass2']){
						$p['pass']=md5($p['pass']);
					}else{
						$info['status']=0;
						$info['msg']='确认密码不正确！';
						$this->ajaxreturn($info);
					}
				}else{
					unset($p['pass']);
				}
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$p['reg_time']=time();
				if($p['pass']==$p['pass2']){
					$p['pass']=md5($p['pass']);
				}else{
					$info['status']=0;
					$info['msg']='确认密码不正确！';
					$this->ajaxreturn($info);
				}
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		
		$role=M('admin_auth_group')->field('id,title')->where(array('status'=>'1'))->order('id asc')->select();
		if($_SESSION['admin']['user']['role']!='1'){
			unset($role[0]);
		}
		$this->assign('role',$role);
		$this->display();
	}

	//删除会员
	public function del(){
		$m=M('admin_user');
		$id=I('id');
		if($id){
			$re=$m->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
	//角色管理
	public function role(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'角色列表',
				'url'=>U('Admin/User/role')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('admin_auth_group');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		if(I('name')){
			$search=I('name');
			$map['name']=array('like',"%$search%");
		}
		$map['status']='1';
		$cache=$m->where($map)->page($p,$psize)->order('id asc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '角色管理','bb-search');
		if($_SESSION['admin']['user']['role']!='1'){
			unset($cache[0]);
		}
		$this->assign('cache',$cache);
		$this->display();
	}
	
	//角色设置
	public function role_set(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'角色管理',
				'url'=>U('Admin/User/role')
			),
			'2'=>array(
				'name'=>'角色设置',
				'url'=>U('Admin/User/role_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('admin_auth_group');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id,'status'=>'1'))->find();
			$this->assign('cache',$data);
		}
		$this->display();
	}
	
	//删除角色
	public function role_del(){
		$m=M('admin_role');
		$id=I('id');
		if($id){
			$re=$m->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
	
//===========================================================================
	//模块管理
	public function moder(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'模块管理',
				'url'=>U('Admin/User/moder')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('admin_auth_rule');
		//$p=$_GET['p']?$_GET['p']:1;
		//$psize=40;//C('BB_PSIZE')?C('BB_PSIZE'):20;
		if(I('name')){
			$search=I('name');
			$map['title']=array('like',"%$search%");
		}
		$map['isnemu']=1;
		$cache=$m->where($map)->order('list_order ASC, id ASC')->select();
		$cache=\bb\Data::tree($cache, "title", "id", "pid");
		$this->assign('cache',$cache);
		//p($cache);
		//$this->getPage(count($cache), $psize, 'bb-loader', '角色管理','bb-search');
		$this->display();
	}
	
	//模块管理
	public function moder_set(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'模块管理',
				'url'=>U('Admin/User/moder')
			),
			'2'=>array(
				'name'=>'模块设置',
				'url'=>U('Admin/User/moder_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('admin_auth_rule');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$data=$p;
				unset($p['add']);unset($p['addid']);
				unset($p['edit']);unset($p['editid']);
				unset($p['del']);unset($p['delid']);
				$p['edittime']=time();
				$re=$m->save($p);
				if($re){
					if($data['add']){
						$add=$p;
						$add['pid']=$add['id'];
						$add['title']='add';
						$add['name']=$add['name'].'_add';
						$add['isnemu']=0;
						unset($add['id']);
						$dat=$m->where(array('name'=>$add['name']))->find();
						if(!$dat){
							$m->add($add);
						}
					}else{
						$m->delete($data['addid']);
					}
					if($data['edit']){
						$edit=$p;
						$edit['pid']=$edit['id'];
						$edit['title']='edit';
						$edit['name']=$edit['name'].'_edit';
						$edit['isnemu']=0;
						unset($edit['id']);
						$dat=$m->where(array('name'=>$edit['name']))->find();
						if(!$dat){
							$m->add($edit);
						}
					}else{
						$m->delete($data['editid']);
					}
					if($data['del']){
						$del=$p;
						$del['pid']=$del['id'];
						$del['title']='del';
						$del['name']=$del['name'].'_del';
						$del['isnemu']=0;
						unset($del['id']);
						$dat=$m->where(array('name'=>$del['name']))->find();
						if(!$dat){
							$m->add($del);
						}
					}else{
						$m->delete($data['delid']);
					}

					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$data=$p;
				unset($p['add']);
				unset($p['edit']);
				unset($p['del']);
				$re=$m->add($p);
				if($re){
					if($data['add']){
						$add=$data;
						$add['pid']=$re;
						$add['title']='add';
						$add['name']=$add['name'].'_add';
						$add['isnemu']=0;
						unset($add['add']);
						unset($add['edit']);
						unset($add['del']);
						$m->add($add);
					}
					if($data['edit']){
						$edit=$data;
						$edit['pid']=$re;
						$edit['title']='edit';
						$edit['name']=$edit['name'].'_edit';
						$edit['isnemu']=0;
						unset($edit['add']);
						unset($edit['edit']);
						unset($edit['del']);
						$m->add($edit);
					}
					if($data['del']){
						$del=$data;
						$del['pid']=$re;
						$del['title']='del';
						$del['name']=$del['name'].'_del';
						$del['isnemu']=0;
						unset($del['add']);
						unset($del['edit']);
						unset($del['del']);
						$m->add($del);
					}
					
					$info['status']=1;
					$info['msg']='添加成功！';
					
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$data['add']=0;
			$data['edit']=0;
			$data['del']=0;
			if($data){
				$operation=$m->where(array('pid'=>$id))->select();
				$this->assign('iscaozhuo',$operation);
				foreach ($operation as $k => $v) {
					if($v['title']=='add'){ $data['add']=1; }
					if($v['title']=='edit'){ $data['edit']=1; }
					if($v['title']=='del'){ $data['del']=1; }
				}
			}
			$this->assign('cache',$data);
		}
		$moders=$m->where(array('isnemu'=>1))->order('list_order ASC, id ASC')->select();
		$moders=\bb\Data::tree($moders, "title", "id", "pid");
		$this->assign('moders',$moders);
		$this->display();
	}

	//删除模块
	public function moder_del(){
		$m=M('admin_auth_rule');
		$id=I('id');
		if($id){
			$rule=$m->where(array('pid'=>$id))->find();
			if($rule){
				$re=$m->where(array('pid'=>$id))->delete();
				$re=$m->delete($id);
			}else{
				$re=$m->delete($id);
			}
			if($re){
				$info['status']=1;
				$info['msg']='删除成功！';
			}else{
				$info['status']=0;
				$info['msg']='删除失败！';
			}
		}else{
			$info['status']=0;
			$info['msg']='删除失败！';
		}
		$this->ajaxreturn($info);
	}
	//===========
	public function moder_px(){
		if(IS_POST){
			$m=M('admin_auth_rule');
			$p=I('post.');
			foreach ($p['list_order'] as $nid => $order) {
	            $m->save(array(
	                "id" => $nid,
	                "list_order" => $order
	            ));
	        }
			$info['status']=1;
			$info['msg']='排序成功！';
	        $this->ajaxreturn($info);
		}
	}
	//权限设置
	public function admin_role_set(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'模块管理',
				'url'=>U('Admin/User/moder')
			),
			'2'=>array(
				'name'=>'权限分配',
				'url'=>U('Admin/User/admin_role_set')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			$role='';
			foreach ($p['nid'] as $k => $v) {
                $role=$role.$v.",";
            }
			$ret['id']=$p['id'];
			$ret['rules']=rtrim($role,',');
			$re=M('admin_auth_group')->save($ret);
			if($re){
				$info['status']=1;
				$info['msg']='设置成功！';
			}else{
				$info['status']=0;
				$info['msg']='设置失败！';
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$m=M('admin_auth_rule');
			$result = $m->select();
			$data=\bb\Data::channelLevel($result, 0, '-', 'id');
			$this->assign('id',$id);
			$this->assign('access',$data);
			$roles=M('admin_auth_group')->field('id,rules')->where(array('id'=>$id,'status'=>'1'))->find();
			$this->assign('role',explode(',',$roles['rules']));	
		}
		$this->display();
	}
	//用户设置自己的信息
	public function user_set(){
		$bread=array(
			'0'=>array(
				'name'=>'账户设置',
				'url'=>'#'
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		$m=M('admin_user');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			if($id){
				if($p['pass']){
					if($p['pass']==$p['pass2']){
						$p['pass']=md5($p['pass']);
					}else{
						$info['status']=0;
						$info['msg']='确认密码不正确！';
						$this->ajaxreturn($info);
					}
				}else{
					unset($p['pass']);
				}
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$info['status']=0;
				$info['msg']='出错了！';
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		$this->display();
	}
	
	//=============================
	public function moder_chaozuo(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'模块管理',
				'url'=>U('Admin/User/moder')
			),
			'2'=>array(
				'name'=>'模块操作管理',
				'url'=>U('Admin/User/moder_chaozuo')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$id=I('id');
		if($id){
			$m=M('admin_auth_rule');
			$moder=$m->where(array('id'=>$id))->find();
			$p=$_GET['p']?$_GET['p']:1;
			$map['pid']=$id;
			$cache=$m->where($map)->page($p,$psize)->order('list_order ASC, id ASC')->select();
			foreach ($cache as $k => $v) {
				$cache[$k]['title']=$moder['title'].$v['title'];
			}
			$this->assign('cache',$cache);
			if(count($cache)){
				$this->getPage(count($cache), $psize, 'bb-loader', '角色管理','bb-search');
			}
			$this->assign('pid',$id);
			$this->display();
		}
	}
	//====================
	public function moder_chaozuoset(){
		$bread=array(
			'0'=>array(
				'name'=>'管理员管理',
				'url'=>U('Admin/User/index')
			),
			'1'=>array(
				'name'=>'模块管理',
				'url'=>U('Admin/User/moder')
			),
			'2'=>array(
				'name'=>'模块操作管理',
				'url'=>U('Admin/User/moder_chaozuo')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('admin_auth_rule');
		$id=I('id');
		$pid=I('pid');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
					
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		if($pid){
			$this->assign('pid',$pid);
			$this->display();
		}else{
			echo '出错了！';
			echo '<a id="alink" href="'.U('User/moder').'" data-loader="bb-loader" data-loadername=""></a>'; 
			echo "<script>initLoader('bb-loader'); setTimeout(function(){ document.getElementById('alink').click(); },1000); </script>";
		}
	}

}